package org.oslims.client.uicomponents.editbox;

import java.util.HashMap;
import java.util.Map;

import org.oslims.client.uicomponents.inputfields.BooleanInputField;
import org.oslims.client.uicomponents.inputfields.DropDownInputField;
import org.oslims.client.uicomponents.inputfields.InputField;
import org.oslims.client.uicomponents.inputfields.SelectManyBox;
import org.oslims.client.uicomponents.inputfields.TextInputField;
import org.oslims.domain.Analysis;
import org.oslims.domain.Parameter;
import org.oslims.domain.SampleType;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class EditAnalysisBox {

	final DialogBox editAnalysisBox = new DialogBox(false, true);
	final VerticalPanel editAnalysisBoxPanel = new VerticalPanel();

	protected Map<String, InputField> inputFields = new HashMap<String, InputField>();

	final HorizontalPanel buttonLine = new HorizontalPanel();
	final Button saveButton = new Button("Save");
	final Button cancelButton = new Button("Cancel");

	final TextInputField nameField = new TextInputField("name", "Name: ", "80px", "200px");
	final TextInputField estTimeField = new TextInputField("estimatedTime", "Estimated Time: ", "80px", "200px");
	final TextInputField estCostField = new TextInputField("estimatedCost", "Estimated Cost", "80px", "200px");
	final DropDownInputField doneInField;
	final SelectManyBox<Parameter> parameterSelectionField = new SelectManyBox<Parameter>("Parameters: ");
	final SelectManyBox<SampleType> sampleTypeSelectionField = new SelectManyBox<SampleType>("Valid sample types: ");

	final BooleanInputField activeField = new BooleanInputField("active", "Active:", "80px", "200px");

	private Analysis analysis;

	String editedType;

	public EditAnalysisBox() {

		doneInField = new DropDownInputField("doneIn", "Done In: ", "80px", "200px");

		editAnalysisBoxPanel.add(nameField);
		inputFields.put(nameField.getFieldName(), nameField);

		editAnalysisBoxPanel.add(estTimeField);
		inputFields.put(estTimeField.getFieldName(), estTimeField);

		editAnalysisBoxPanel.add(estCostField);
		inputFields.put(estCostField.getFieldName(), estCostField);

		editAnalysisBoxPanel.add(doneInField);
		inputFields.put(doneInField.getFieldName(), doneInField);

		editAnalysisBoxPanel.add(parameterSelectionField);

		editAnalysisBoxPanel.add(sampleTypeSelectionField);

		editAnalysisBoxPanel.add(activeField);
		inputFields.put(activeField.getFieldName(), activeField);

		buttonLine.add(saveButton);
		buttonLine.add(cancelButton);

		editAnalysisBox.add(editAnalysisBoxPanel);

		// Add the save and cancel buttons
		editAnalysisBoxPanel.add(buttonLine);
	}

	public void hide() {
		editAnalysisBox.hide();
	}

	public void clear() {
		analysis = null;
		nameField.getInputField().setValue("");
		estTimeField.getInputField().setValue("");
		estCostField.getInputField().setValue("");
		doneInField.getInputBox().setSelectedIndex(-1);
		activeField.getInputField().setValue(Boolean.FALSE);
	}

	public void showNew() {
		this.clear();
		this.clearAllErrors();
		editAnalysisBox.setText("Create new Analysis");
		editAnalysisBox.center();
	}

	public void showEdit(Analysis a) {
		this.clear();
		this.clearAllErrors();

		analysis = a;

		nameField.getInputField().setValue(a.getName());
		if (a.getEstimatedCost() != null)
			estCostField.getInputField().setValue(a.getEstimatedCost().toString());

		if (a.getEstimatedTime() != null)
			estTimeField.getInputField().setValue(a.getEstimatedTime().toString());

		for (int i = 0; i < doneInField.getInputBox().getItemCount(); i++) {

			if (doneInField.getInputBox().getItemText(i).equals(a.getDoneIn().getName())) {

				doneInField.getInputBox().setSelectedIndex(i);
				break;
			}
		}

		if (a.getActive() != null)
			activeField.getInputField().setValue(a.getActive());

		editAnalysisBox.setText("Edit Analysis");
		editAnalysisBox.center();
	}

	public void showError(String fieldName, String message) {
		GWT.log(fieldName);
		inputFields.get(fieldName).showError(message);
	}

	public void clearError(String fieldName) {
		inputFields.get(fieldName).clearError();
	}

	public void clearAllErrors() {
		for (InputField inputField : inputFields.values()) {
			inputField.clearError();
		}
	}

	public HasValue<String> getName() {
		return nameField.getInputField();
	}

	public HasValue<String> getEstimatedTime() {
		return estTimeField.getInputField();
	}

	public HasValue<String> getEstimatedCost() {
		return estCostField.getInputField();
	}

	public ListBox getDepartment() {
		return doneInField.getInputBox();
	}

	public SelectManyBox<Parameter> getParameterSelection() {
		return parameterSelectionField;
	}

	public SelectManyBox<SampleType> getSampleTypeSelection() {
		return sampleTypeSelectionField;
	}

	public HasValue<Boolean> getActive() {
		return activeField.getInputField();
	}

	public HasClickHandlers getSaveButton() {
		return saveButton;
	}

	public HasClickHandlers getCancelButton() {
		return cancelButton;
	}

	public void setAnalysis(Analysis analysis) {
		this.analysis = analysis;
	}

	public Analysis getAnalysis() {
		return analysis;
	}
}
